package com.aidex.biz.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.aidex.biz.service.SSEService;
import com.aidex.biz.utils.AliQwenUtils;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationParam;
import com.alibaba.dashscope.app.ApplicationResult;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;

import javax.validation.constraints.*;

import io.reactivex.Flowable;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.ProductInfo;
import com.aidex.biz.service.ProductInfoService;
import com.aidex.common.utils.SecurityUtils;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * 贷款产品Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-06-20
 */
@RestController
@RequestMapping("/biz/bizProductInfo")
public class ProductInfoController extends BaseController {
    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private SSEService sseService;


    /**
     * 查询贷款产品列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:list')")
    @GetMapping("/list")
    public R<PageInfo> list(ProductInfo productInfo, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            productInfo.setCompanyId(null);
        } else {
            productInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        productInfo.setPage(new PageDomain(request, response));
        return R.data(productInfoService.findPage(productInfo));
    }

    /**
     * 获取贷款产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:query')")
    @GetMapping(value = "/{id}")
    public R<ProductInfo> detail(@PathVariable("id") String id) {
        return R.data(productInfoService.get(id));
    }

    /**
     * 新增贷款产品
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:add')")
    @Log(title = "贷款产品", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated ProductInfo productInfo) {
        return R.status(productInfoService.save(productInfo));
    }

    /**
     * 修改贷款产品
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:edit')")
    @Log(title = "贷款产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated ProductInfo productInfo) {
        return R.status(productInfoService.save(productInfo));
    }


    /**
     * 删除贷款产品
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:remove')")
    @Log(title = "贷款产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(productInfoService.deleteProductInfoByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(productInfoService.findMaxSort(new ProductInfo()));
    }

    /**
     * 导出贷款产品列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:export')")
    @Log(title = "贷款产品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(ProductInfo productInfo) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            productInfo.setCompanyId(null);
        } else {
            productInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<ProductInfo> list = productInfoService.findList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        return util.exportExcel(list, "贷款产品数据");
    }


}
